package com.appspot.androidquote.webapp;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.androidquote.webapp.Entities.MessageEntity;
import com.appspot.androidquote.webapp.dao.MessageDao;

@SuppressWarnings("serial")
public class ServletCreateMessage extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Creating new message");
		
		MessageEntity messageEntity = new MessageEntity();
		messageEntity.setCategory(checkNull(req.getParameter("category")));
		messageEntity.setAuthor(checkNull(req.getParameter("author")));
		messageEntity.setMessage(checkNull(req.getParameter("message")));
		messageEntity.setEnabled(true);
		
		MessageDao.INSTANCE.add(messageEntity);
		
		resp.sendRedirect("/Messages.jsp");
	}
	
	private String checkNull(String s) {
		if (s == null)
			return "";
		else
			return s;
	}
}
